package com.speedata.testsimple;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.serialport.DeviceControlSpd;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.speedata.uhftest.R;
import com.speedata.utils.ToastUtils;

import java.io.IOException;


/**
 * @author xu  发广播
 * @date 2023/08/14
 */

public class SendBroadcastAct extends AppCompatActivity implements View.OnClickListener {

    /**
     *
     */
    private Button bt2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sendbroadcast);
        initView();
        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(usbReceiver, filter);
    }


    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                // USB设备已连接
                ToastUtils.showShortToastSafe("USB设备已连接");
            } else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                // USB设备已断开连接
                ToastUtils.showShortToastSafe("USB设备已断开连接");
                // TODO: 2023/8/17/017 拉低175再拉高175
                releaseDMRModule6762();
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                initPower6762();

            }
        }
    };

    private DeviceControlSpd DevCtrl;

    public void initPower6762() {
        try {
            //175
            DevCtrl = new DeviceControlSpd(DeviceControlSpd.PowerType.NEW_MAIN_FG);
            DevCtrl.newFgSetGpioOn(175);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void releaseDMRModule6762() {
        //175
        if (DevCtrl != null) {
            try {
                DevCtrl.newFgSetGpioOff(175);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Override
    protected void onDestroy() {
        unregisterReceiver(usbReceiver);
        super.onDestroy();
    }

    private void initView() {

        bt2 = findViewById(R.id.bt2);
        bt2.setOnClickListener(this);

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt2:
                try {

                    Intent intent = new Intent("ACTION_BARCODE_CFG");
                    intent.putExtra("CODE_ID", "EAN8");
                    intent.putExtra("PROPERTY", "TrsmtChkChar");
                    intent.putExtra("VALUE", "1"); // “1” Enable EAN-8, ”0” Disable EAN-8
                    sendBroadcast(intent);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }


}
